import QtQuick 2.0

Item {
    visible: true
    width: 640
    height: 480
    Keys.enabled: true
    focus: true

    //盲僧技能演示
    //鼠标右键移动玩家
    //按下Q键,显示技能条
    //鼠标移动控制技能指向
    //鼠标左键,释放技能
    //技能命中目标,自动把玩家带到目标处

    Keys.onPressed: {
        if(event.key === Qt.Key_Q){
            button_q_press = true
            event.accepted = true;
        }
    }

    property bool button_q_press: false
    property int skillState: 0  //0未激活,1激活,2发射,3命中

    onButton_q_pressChanged: {
        if(button_q_press){
            skillState = 1
        }
    }

//    Rectangle{
//        anchors.fill: parent
//        color: "gray"
//    }

    Text {
        anchors.right: parent.right
        anchors.top: parent.top
        horizontalAlignment: Text.AlignRight
        text: qsTr("盲僧技能演示\n鼠标右键移动玩家\n按下Q键,显示技能条\n鼠标移动控制技能指向\n鼠标左键,释放技能\n技能命中目标,自动把玩家带到目标处")
    }

    Unit{
        id: unitYan
        anchors.centerIn: parent
        src: "qrc:/enemy.png"

        property point center: Qt.point(x+width/2, y+height/2)
        property real sradius: width/2
    }

    Rectangle{
        id: rectDisSkill
        width: 45
        height: width
        radius: 50
        color: "yellow"
        z: 1
        visible: false

        property point center: Qt.point(rectDisSkill.x+width/2, rectDisSkill.y+height/2)
        property real sradius: width/2
        property bool benabled: true

        Behavior on x{
            id: bx
            enabled: rectDisSkill.benabled
            NumberAnimation{ duration: 300; }
        }
        Behavior on y{
            id: by
            enabled: rectDisSkill.benabled
            NumberAnimation{ duration: 300; }
        }
    }

    Player{
        id: player
        x: parent.width/2-width/2
        y: parent.height/3*2

        property point center: Qt.point(x+width/2, y+height/2)
        property real radius: width/2

        property point targetPoint
        property point skillTargetPoint
        property real xTime: 0
        property real moveSpeed: 0.5
        property real skillRotation: 0
        property alias skill: skill

        onTargetPointChanged: {
//            console.log("onTargetPointChanged ", targetPoint.x, targetPoint.y)
            var targetX = targetPoint.x - player.width/2
            var targetY = targetPoint.y - player.height/2

            var distance = Math.sqrt((targetX - player.x) * (targetX - player.x) + (targetY - player.y) * (targetY - player.y));
            xTime = distance / player.moveSpeed

            player.x = targetX
            player.y = targetY
        }

        onSkillTargetPointChanged: {
            var targetX = skillTargetPoint.x - player.radius
            var targetY = skillTargetPoint.y - player.radius

            var radian = Math.atan2((targetY - player.y), (targetX - player.x) );
//            console.log("radian ", radian)
            skillRotation = radian/Math.PI * 180;
        }

        Behavior on x{
            NumberAnimation{ duration: player.xTime; }
        }
        Behavior on y{
            NumberAnimation{ duration: player.xTime; }
        }

        Item{
            id: skill
            anchors.centerIn: parent
            visible: false

            onVisibleChanged: {
                if(visible == false){
                    //技能发动
                    skillState = 2;
                    var degree = rotation/180*Math.PI
                    var targetY = Math.sin(degree) * img.width
                    var targetX = Math.cos(degree) * img.width
                    rectDisSkill.benabled = true;
                    rectDisSkill.x += targetX
                    rectDisSkill.y += targetY
//                    console.log("rectDisSkill ", degree, targetY, targetX, rectDisSkill.x, rectDisSkill.y )
                }
                else{
                    rectDisSkill.benabled = false;
                    rectDisSkill.x = player.center.x - rectDisSkill.width/2;
                    rectDisSkill.y = player.center.y - rectDisSkill.height/2;
                }
            }

            rotation: player.skillRotation
//            onRotationChanged: console.log("rotation ", rotation)
//            Rectangle{
//                width: 136
//                height: 45
//                y: -height/2
//                color: "green"
//            }
            Image {
                id: img
                y: -height/2
                source: "qrc:/skill.png"
            }
        }
    }

    function checkPengZhuang(pa, ra, pb, rb){
        var distance = Math.sqrt((pa.x-pb.x)*(pa.x-pb.x)+(pa.y-pb.y)*(pa.y-pb.y))
//        console.log("checkPengZhuang ", pa, pb, ra+rb, distance)
        if(ra+rb >= distance){
            return true;
        }
        return false
    }

    Timer{
        interval: 30
        repeat: true
        running: true
        onTriggered: {
            if(button_q_press){
                player.skillTargetPoint = mousearea.mousePoint;
                player.skill.visible = true;
            }

            //碰撞
            if(skillState == 2 && checkPengZhuang(rectDisSkill.center, rectDisSkill.sradius, unitYan.center, unitYan.sradius)){
                console.log("碰撞")
                skillState = 3;

                //飞过去到目标处
                player.targetPoint = Qt.point(unitYan.center.x, unitYan.center.y);
            }
        }
    }

    MouseArea{
        id: mousearea
        anchors.fill: parent
        acceptedButtons: Qt.LeftButton | Qt.RightButton
        hoverEnabled: true

        property point mousePoint

        onPressed: {
            if(mouse.button == Qt.RightButton){
                player.targetPoint = Qt.point(mouse.x, mouse.y);
                console.log("Qt.RightButton ", player.targetPoint)
            }
            else{
                console.log("Qt.LeftButton")
                button_q_press = false;
                player.skill.visible = false;
            }
        }
        onPositionChanged: {
            mousePoint = Qt.point(mouse.x, mouse.y);
        }
    }
}
